﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System.Web;
using DNA.Mvc.Data.Entity;
using DNA.Mvc.Data.Xml;

namespace DNA.Mvc.Data
{
    public class DataContext : UnitOfWorkBase<DB>, IDataContext
    {
        public DataContext()
        {
            this.dbContext = new DB();
        }

        private IWebRepository webRepository = null;
        private IWebPageRepository webPageRepository = null;
        private IRepository<WebPageRole> webPageRoleRepository = null;
        private IWidgetRepository widgetRepository = null;
        private IWidgetDescriptorRepository widgetDescriptorRepository = null;
        private IPermissionRepository permissionRepository = null;
        private IPermissionsetRepository permissionsetRepository = null;
        private IOpenIDRepository openidRepository = null;
        private IPackageRepository themeRepository = null;
        private IPackageRepository solutionRepository = null;
        private IPackageRepository tempRepository = null;
        private IWidgetTemplateRepository widgetTemplateRepository = null;
        private IPrivateMessageRepository privateMessageRepository = null;
        private IFriendRepository friendRepository = null;

        public virtual IWebRepository Webs
        {
            get
            {
                if (webRepository == null)
                    webRepository = new WebRepository(this.dbContext);
                return webRepository;
            }
        }

        public virtual IWebPageRepository WebPages
        {
            get
            {
                if (webPageRepository == null)
                    webPageRepository = new WebPageRepository(this.dbContext);
                return webPageRepository;
            }
        }

        public virtual IWidgetRepository Widgets
        {
            get
            {
                if (widgetRepository == null)
                    widgetRepository = new WidgetRepository(this.dbContext);
                return widgetRepository;
            }
        }

        public virtual IWidgetDescriptorRepository WidgetDescriptors
        {
            get
            {
                if (widgetDescriptorRepository == null)
                    widgetDescriptorRepository = new WidgetDescriptorRepository(this.dbContext);
                return widgetDescriptorRepository;
            }
        }

        public virtual IPermissionRepository Permissions
        {
            get
            {
                if (permissionRepository == null)
                    permissionRepository = new PermissionRepository(this.dbContext);
                return permissionRepository;
            }
        }

        //public virtual ISubscriptionRepository Subscriptions
        //{
        //    get
        //    {
        //        if (subscriptionRepository == null)
        //            subscriptionRepository = new SubscriptionRepository(this.dbContext);
        //        return subscriptionRepository;
        //    }
        //}

        public virtual IPrivateMessageRepository PrivateMessages
        {
            get
            {
                if (privateMessageRepository == null)
                    privateMessageRepository = new PrivateMessageRepository(this.dbContext);
                return privateMessageRepository;
            }
        }

        public virtual IFriendRepository Friends 
        {
            get 
            {
                if (friendRepository == null)
                    friendRepository = new FriendRepository(this.dbContext);
                return friendRepository;
            }
        }

        public virtual IOpenIDRepository OpenIDs
        {
            get
            {
                if (openidRepository == null)
                    openidRepository = new OpenIDRepository(this.dbContext);
                return openidRepository;
            }
        }

        public virtual IPermissionsetRepository PermissionSets
        {
            get
            {
                if (permissionsetRepository == null)
                    permissionsetRepository = new PermissionsetRepository(this.dbContext);
                return permissionsetRepository;
            }
        }

        public virtual IPackageRepository Themes
        {
            get
            {
                if (themeRepository == null)
                {
                    // var path=HttpContext.Current.Server.MapPath("~/content/themes");
                    themeRepository = new PackageXmlRepository("~/content/themes");
                }
                return themeRepository;
            }
        }

        public virtual IPackageRepository Solutions
        {
            get
            {
                if (solutionRepository == null)
                {
                    //var path = HttpContext.Current.Server.MapPath("~/shared/packages/");
                    solutionRepository = new PackageXmlRepository("~/shared/packages/");
                }
                return solutionRepository;
            }
        }

        public virtual IWidgetTemplateRepository WidgetTemplates
        {
            get
            {
                if (widgetTemplateRepository == null)
                {
                    var path = HttpContext.Current.Server.MapPath("~/content/widgets/");
                    widgetTemplateRepository = new WidgetTemplateXmlRepository(path);
                }
                return widgetTemplateRepository;
            }
        }

        public IPackageRepository Temporary
        {
            get
            {
                if (tempRepository == null)
                    tempRepository = new PackageXmlRepository("~/shared/temp/");
                return tempRepository;
            }
        }

        protected override EntityRepositoryBase<DB, T> GenericRepostory<T>()
        {
            return new EntityRepository<T>(this.dbContext);
        }


        public IRepository<WebPageRole> WebPageRoles
        {
            get 
            {
                if (webPageRoleRepository == null)
                    webPageRoleRepository = GenericRepostory<WebPageRole>();
                return webPageRoleRepository;
            }
        }
    }
}
